<?php

/**
 * Object used to manage customers and their information
 * 
 * @package Customer
 * @author Guillem Mateos
 * @version 0.1
 * @abstract 
 * @copyright GRITO2003, S.L.
 */

/**
 * includes
 */
//include_once( '../library/config.php' );
include_once( FROG_DATABASE.'DBObject.class.php' );

class User extends DBObject
{
  /**
  * Initialize vars
  */
  public function initVars( )
  {
    $this->dbfields['id_user'] = null;
    $this->dbfields['email'] = null;
    $this->dbfields['passwd'] = null;
    $this->dbfields['name'] = null;
    $this->dbfields['surname'] = null;
    
    $this->dbparams['table'][0] = 'user';
    $this->dbparams['main_id'] = 'id_user';
  }
  
  public static function findByEmail( $email )
  {
    if( !$email )
      return false;
      
    $c = new User();
        
    $w_expr_01 = new Expression( 'email', '=', $email );

    $result = $c->select( '', $w_expr_01  );
    
    return $result[0];
  }
  
  public static function login( $email, $passwd, $sess_var = 'email' )
  {
    $user = User::findByEmail( $email );
    
    if ( $user && $passwd )
    {
      if ( $user->getPasswd() == md5( $passwd ) )
      {
        $_SESSION[$sess_var] = $email;
        return $user;
      }
    }
    
    return false;
  }
  
  public static function checkLogged( $sess_var = 'email' )
  {
    if ( !isset( $_SESSION[$sess_var] ) )
      return false;
    else
      return true;
  }
} 

?>